package com.gxa.servlet.goods;

import com.alibaba.fastjson.JSON;
import com.gxa.pojo.Result;
import com.gxa.pojo.goods.GoodsOneType;
import com.gxa.pojo.goods.Type;
import com.gxa.service.goods.GoodsOneTypeService;
import com.gxa.service.goods.GoodsTypeService;
import com.gxa.service.goods.impl.GoodsOneTypeServiceimpl;
import com.gxa.service.goods.impl.GoodsTypeServiceimpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(urlPatterns = "/GoodsOneTypeServlet")
public class GoodsOneTypeServlet extends HttpServlet {
    GoodsOneTypeService goodsOneTypeService = new GoodsOneTypeServiceimpl();
    GoodsTypeService goodsTypeService = new GoodsTypeServiceimpl();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        //因为request.getreader（）方法只能读一次，采样下面几行代码
//        BufferedReader streamReader = new BufferedReader( new InputStreamReader(request.getInputStream(), "UTF-8"));
//        StringBuilder json = new StringBuilder();
//        String inputStr;
//        while ((inputStr = streamReader.readLine()) != null) {
//            json.append(inputStr);
//        }
//        String s = json.toString(); //这是将json转化为字符串的形式
        String onetypename = request.getParameter("onetypename");
        // GoodsOneType oneType = JSON.parseObject(s,GoodsOneType.class);
//        String onetypename = oneType.getOnetypename();
        int insert = goodsOneTypeService.insert(onetypename);
        Type type = new Type();
        type.setFather("无");
        type.setTypename(onetypename);
        type.setLevel("一级");
        goodsTypeService.insert(type);
        response.setContentType("text/json;charset=utf-8");
        Map<String, Object> map = new HashMap<>();
        if (insert!=0){
            map.put("Result",Result.ok("添加成功"));
        }else {
            map.put("Result",Result.error());
        }
        String s1 = JSON.toJSONString(map);
        response.getWriter().write(s1);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doGet(request,response);
    }
}
